package uk.ac.ncl.carrent;

import java.util.*;

/**
 * Person - a person with fields first name, last name and date of birth
 * @author Shiwei He 
 */

public class Person{
	
	private final String firstName;
	
	private final String lastName;
	
	private final Calendar dob;
	
	/**
	 * Construct a person from give name, date of birth and licence type.
	 *
	 * @param firstName the person first name
	 * @param lastName the person last name
	 * @param dob the date of birth
	 * @param isFullLicence if full licence true
     * @throws IllegalArgumentException if either
     * <code>name</code> or <code>dob</code> is
     * empty
	 */
	public Person(String firstName, String lastName, String birth){
		if (firstName == null) {
			throw new IllegalArgumentException("First name is empty");
		}
		if (lastName == null) {
			throw new IllegalArgumentException("Last name is empty");
		}
		if (birth == null) {
			throw new IllegalArgumentException("Date of birth is empty");
		}
		this.firstName = firstName;
		this.lastName = lastName; 
		final String parts[] = birth.split("/");
		final int year=Integer.parseInt(parts[0]);
		final int month=Integer.parseInt(parts[1]);
		final int date=Integer.parseInt(parts[2]);		
		dob = new GregorianCalendar (year, month, date);
	}
	
	/**
	 * get the first name of the person
	 * @return the first name of the person
	 */
	
	public String getFirstName(){
		return firstName;
	}

	/**
	 * get the last name of the person
	 * @return the last name of the person
	 */
	public String getLastName(){
		return lastName;
	}
	
	/**
	 * get the date of birth
	 * @return the date of birth of the perso
	 */
	public Calendar getDob(){
    	return dob;
    }
	
	/**
	 * get the age of the person
	 * @return the age of the person
	 */
	public int getAge(){
		Calendar rightNow = Calendar.getInstance();
		int age = 0;
		age = rightNow.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
		if(rightNow.get(Calendar.MONTH) >= dob.get(Calendar.MONTH) && rightNow.get(Calendar.DATE) >= dob.get(Calendar.DATE)){
			return age;
		}
		else{
        return age - 1;
		}
	}

	/**
	 * get the initials of the person's name
	 * @return the initials of the person
	 */
	public String getInitials(){
		String initials = firstName.substring(0, 1) + lastName.substring(0, 1);
		return initials.toUpperCase();	
	}
	
	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof Person))
			return false;
		final Person person = (Person) obj;
		return person.getFirstName().equals(getFirstName()) 
				&& person.getLastName().equals(getLastName()) 
				&& person.getDob().equals(dob);
	}	
	
	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		int result = 17;
		result = 37 * result + getFirstName().hashCode();
		result = 37 * result + getLastName().hashCode();
		result = 37 * result + dob.hashCode();
		return result;
	}

	/**
	 * get a string representation of the person
	 * Name: firstName lastName; Date of birth: dob
	 * @see java.lang.Object#toString()
	 */
	public String toString(){
		return "Name:"+ firstName + " " + lastName + "; " 
				+ "Date of birth:" + dob;
	}
	
}